﻿using Loong.Threading.Locks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Loong.Web.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class LockController : ControllerBase
    {
        private readonly ILockFactory _lockFactory;

        public LockController(ILockFactory lockFactory)
        {
            _lockFactory = lockFactory;
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<string> Get()
        {
            using (await _lockFactory.LockAsync("test", _lockFactory.GetOrCreateOwner()))
            {
                await Task.Delay(TimeSpan.FromSeconds(20));
                return "ok";
            }
        }
    }
}
